import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'

import { getCouponShopList, getCardShopList } from "@src/api/coupon";

Component({
  options: {
    addGlobalClass: true
  },

  data: {
    allShopCanUse: 0,
    listData: []
  },
  methods: {
    getListData(type, id) {
      //券
      if (type == 1) {
        getCouponShopList(id).then(res => {
          if (res.code == 0) {
            this.handleData(res.data)
            console.log('res', res)
          }
        })
      } else if (type == 2) {//卡
        getCardShopList(id).then(res => {
          console.log('res', res)
          if (res.code == 0) {
            this.handleData(res.data)
          }
        })
      }
    },
    handleData(list) {
      let arr = [];
      if (list.length > 0) {
        list.forEach(item => {
          if (item.shops) {
            arr.push(...item.shops)
          }
        })
        this.setData({
          listData: arr
        })
      }
    },
    getAllShopList() {
      /*暂停刷新*/
      wx.stopPullDownRefresh();
      let data = {
        latitude: this.data.latitude,
        longitude: this.data.longitude
      };
      getCouponAllShopList(data).then(res => {
        if (res.code === 0) {
          let list = res.data;
          this.setData({ listData: list });
          console.log('listData', this.data.listData)
        }
      })
    },
    getAailableShopList() {
      let data = {
        couponId: this.data.couponId,
        latitude: this.data.latitude,
        longitude: this.data.longitude
      };
      getCouponAvailableShopList(data).then(res => {
        if (res.code === 0) {
          let list = res.data;
          this.setData({ listData: list });
          console.log('listData', this.data.listData)
        }
      })
    },
    //打电话按钮
    callPhoneBtn(e) {
      if (e.currentTarget.dataset.phone) {
        wx.makePhoneCall({ phoneNumber: e.currentTarget.dataset.phone });
      }
    },
    //地图
    // openMap(e) {
    //   let data = e.currentTarget.dataset.shop;
    //   wx.openLocation({
    //     latitude: data.latitude,
    //     longitude: data.longitude,
    //     name: data.name,
    //     address: data.address
    //   });
    // },
    dialogShow(passData) {
      this.setData({
        shopListDialog: true
      })
      this.getListData(passData.type, passData.couponId);
    },
    close() {
      this.setData({ shopListDialog: false, listData: [] })
    },
    handleStop() {
      return false;
    }
  }
})
